package com.fzebu.fastboot.util;

import com.fzebu.fastboot.annotations.form.Table;
import com.fzebu.fastboot.form.FormClass;
import com.fzebu.fastboot.form.converter.FormConverter;
import com.fzebu.fastboot.form.converter.impl.ReqFormHandler;
import com.fzebu.fastboot.form.exception.ConvertException;
import com.fzebu.fastboot.workflow.WorkflowBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.DetailTableInfo;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.RequestService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jack
 * @version 1.0.0
 * @create 2020/1/26 19:30
 */
@Slf4j(topic = "debug")
public final class ReqUtil {

    public static <T> T convert(RequestInfo request, Class<T> clazz) throws ConvertException {
        FormConverter converter = FormConverter.builder(clazz)
                .setProperty(request.getMainTableInfo().getProperty())
                .build();
        //noinspection unchecked
        return (T) converter.toJavaBean(new ReqFormHandler());
    }

    public static <T> List<T> convertList(RequestInfo request, Class<T> clazz) throws ConvertException {
        Table table = clazz.getAnnotation(Table.class);
        if (table == null) {
            return new ArrayList<>(0);
        }
        int detailId = table.detailId();
        DetailTableInfo detailTableInfo = request.getDetailTableInfo();
        if (detailTableInfo.getDetailTableCount() < detailId) {
            return new ArrayList<>(0);
        }
        DetailTable detailTable = detailTableInfo.getDetailTable(detailId);
        return convertList(detailTable, clazz);
    }

    public static <T> List<T> convertList(DetailTable detailTable, Class<T> clazz) throws ConvertException {
        FormConverter converter = FormConverter.builder(clazz)
                .detailTable(detailTable)
                .build();
        //noinspection unchecked
        return (List<T>) converter.toJavaArray(new ReqFormHandler());
    }

    public static String getFormClass(String tableName) {
        if (StringUtils.isEmpty(tableName)) {
            return null;
        }
        FormClass formClass = new FormClass.Builder()
                .tableName(tableName)
                .build();
        return formClass.getClassText();
    }

    public static int getBillId(String requestId, String tableName) {
        String sql = "select id from " + tableName + " where requestid=?";
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql, requestId);
        if (rs.next()) {
            return rs.getInt("id");
        }
        return -1;
    }

//    public static boolean nextNodeBySubmit(int requestId) {
//        return nextNodeBySubmit(requestId, new RequestInfo());
//    }
//
//    public static boolean nextNodeBySubmit(int requestId, RequestInfo request) {
//        RequestService service = new RequestService();
//        if (request == null) {
//            request = new RequestInfo();
//        }
//        return service.nextNodeBySubmit(request, requestId, 1, "");
//    }
//
//    public static boolean nextNodeBySubmit(int requestId, Map<String, String> map) {
//        MainTableInfo mainTableInfo = new MainTableInfo();
//        Property prop;
//        for (Map.Entry<String, String> entry : map.entrySet()) {
//            prop = new Property();
//            prop.setName(entry.getKey());
//            prop.setValue(entry.getValue());
//            mainTableInfo.addProperty(prop);
//        }
//        RequestInfo req = new RequestInfo();
//        req.setMainTableInfo(mainTableInfo);
//        return nextNodeBySubmit(requestId, req);
//    }

    public static String createWorkflow(WorkflowBuilder builder) {
        RequestService service = new RequestService();
        try {
            String requestId = service.createRequest(builder.getRequest());
            if (!"".equals(requestId) && Integer.parseInt(requestId) > 0) {
                log.debug("流程创建成功 => requestId: {}", requestId);
            } else {
                log.error("流程创建失败 => msg: {}", requestId);
            }
            return requestId;
        } catch (Exception e) {
            log.error("流程创建失败 => msg: {}", e.getMessage());
            return null;
        }
    }
}